package com.fancy.learncenter.utils;

import android.net.Uri;
import android.os.Environment;

import com.fancy.learncenter.common.Constant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shuyu on 2016/11/15.
 * 文件管理
 */

public class FileUtils {
    private static final String SD_PATH = Environment.getExternalStorageDirectory().getPath();
    private static final String DATA_PATH = Environment.getDataDirectory().getPath();
    private static final String SD_STATE = Environment.getExternalStorageState();

    public static String getAppPath(String urlPath) {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(urlPath);
        return sb.toString();
    }

    // 获取录音存取的根目录
    public static String getPicPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.HEADER_CUT_PATH);
        sb.append(File.separator);
        return sb.toString();
    }

    // 获取录音存取的根目录
    public static String getRecorderAudioPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.RECORDER_AUDIO_PATH);
        sb.append(File.separator);
        return sb.toString();
    }

    /**
     * 学生端，所有题目资源
     *
     * @return
     */
    public static String getSutendRes() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_RES);
        return sb.toString();
    }

    /**
     * 学生端本地录音，属性地址
     *
     * @return
     */
    public static String getSutendLocalRes() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_RECODER_RES);
        return sb.toString();
    }

    /**
     * 每日听说，下所有录音资源
     *
     * @return
     */
    public static String getEveryDayRecorderAudioPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_RECODER);
        sb.append(File.separator);
        return sb.toString();
    }


    /**
     * 绘本阅读，下所有录音资源
     *
     * @return
     */
    public static String getReaderAudioPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_READER_RECODER);
        sb.append(File.separator);
        return sb.toString();
    }

    /**
     * 拓展训练，下所有录音资源
     *
     * @return
     */
    public static String getExpandRecorderAudioPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_EXPAND_RECODER);
        sb.append(File.separator);
        return sb.toString();
    }

    /**
     * 视频缓存地址
     *
     * @return
     */
    public static String getCartoonPath() {
        StringBuilder sb = new StringBuilder();
        if (SD_STATE.equals(
                Environment.MEDIA_MOUNTED)) {
            sb.append(SD_PATH);
        } else {
            sb.append(DATA_PATH);
        }
        sb.append(File.separator);
        sb.append(Constant.CARTOON_VIDEO);
        sb.append(File.separator);
        return sb.toString();
    }


    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else {
                String[] filePaths = file.list();
                for (String path : filePaths) {
                    deleteFile(filePath + File.separator + path);
                }
                file.delete();
            }
        }
    }

    public static int getDirNum(String file_path) {

        File file = new File(file_path);
        if (!file.exists()) {
            return 0;
        } else {
            File[] files = file.listFiles();
            int i = files.length;
            return i;

        }
    }


    public static boolean creatFile(String urlPath) {
//        urlPath = getAppPath(urlPath);
        File file = new File(urlPath);

        if (urlPath.endsWith(File.separator)) {
            //文件路径不能用文件夹路径格式
            return false;
        }
        //父目录是否存在
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            if (file.createNewFile()) {// 创建目标文件
                // 创建文件成功:";
                return true;
            } else {
                // "创建文件失败！
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean creatFileDir(String urlPath) {
        urlPath = getAppPath(urlPath);
        File dir = new File(urlPath);

        if (!urlPath.endsWith(File.separator)) {
            //路径必须为文件夹路径格式
            return false;
        }
        if (dir.mkdirs()) {
            return true;
        }
        return false;
    }

    // 创建目录
    public static boolean createDir(String destDirName) {
//        destDirName = getAppPath(destDirName);
        File dir = new File(destDirName);
        if (dir.exists()) {// 判断目录是否存在
            System.out.println("创建目录失败，目标目录已存在！");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {// 结尾是否以"/"结束
            destDirName = destDirName + File.separator;
        }
        if (dir.mkdirs()) {// 创建目标目录
            System.out.println("创建目录成功！" + destDirName);
            return true;
        } else {
            System.out.println("创建目录失败！");
            return false;
        }
    }

    public static Uri getUri(String urlPath) {
        urlPath = getAppPath(urlPath);

        return Uri.parse(urlPath);
    }


    /**
     * 保存字符串到文件中
     *
     * @param fileName
     * @param content
     */
    public static void saveStringToFile(String fileName, String content) {
        try {
            FileOutputStream outputStream = new FileOutputStream(fileName);
            outputStream.write(content.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 读取文件中的字符串
     *
     * @param fileName
     */
    public static String readFromSD(String fileName) {
        StringBuilder sb = new StringBuilder("");
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(fileName);
            byte[] temp = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(temp)) > 0) {
                sb.append(new String(temp, 0, len));
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static boolean checKFileExists(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
//        Log.e("TAG", "文件存在不用下载了");
            return true;
        }
        return false;
    }


    /**
     * 获取  每日听说  本地录音文件 路径
     *
     * @return
     */
    public static ArrayList<String> getEveryDayRecordAudioPath() {
        ArrayList<String> filsList = new ArrayList<>();
        File files = new File(getEveryDayRecorderAudioPath());
        String[] filesDatas = files.list();

        if (filesDatas == null) {
            return filsList;
        }
        for (int i = 0; i < filesDatas.length; i++) {
            filsList.add(getEveryDayRecorderAudioPath() + filesDatas[i]);
        }
        return filsList;
    }


    /**
     * 获取  每日听说  本地录音文件名
     *
     * @return
     */
    public static ArrayList<String> getEveryDayRecordAudioName() {
        ArrayList<String> filsList = new ArrayList<>();
        File files = new File(getEveryDayRecorderAudioPath());
        String[] filesDatas = files.list();

        if (filesDatas == null) {
            return filsList;
        }
        for (int i = 0; i < filesDatas.length; i++) {
            filsList.add(filesDatas[i]);
        }
        return filsList;
    }


    /**
     * 合成
     *
     * @param outFile
     * @param inFiles
     * @return
     * @throws Exception
     */
    public static boolean combine(String outFile, List<String> inFiles) throws Exception {
        File out = new File(outFile);
        File[] files = new File[inFiles.size()];
        for (int i = 0; i < files.length; i++) {
            files[i] = new File(inFiles.get(i));
        }
        FileInputStream fis = null;
        FileOutputStream fos = new FileOutputStream(outFile, true); // 合并其实就是文件的续写,写成true
        for (int i = 0; i < files.length; i++) {
            fis = new FileInputStream(files[i]);
            int len = 0;
            for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1; ) {
                fos.write(buf, 0, len);
            }
        }
        fis.close();
        fos.close();

        return true;
    }

    public static boolean checkFile(String filePath) {
        File file = new File(filePath);

        if (filePath.endsWith(File.separator)) {
            //文件路径不能用文件夹路径格式
            return false;
        }
        //父目录是否存在
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            if (!file.exists()) {
                file.createNewFile();
            } else {
                // "创建文件失败！
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
